//index.js
const app = getApp()

 const Api = require("../../utils/api.js").API
 const Store = require("../../utils/store.js").store

Page({
  data: {
    userInfo: {},
    isPhone:false,
    code:"",
    encryptedData:'',
    iv:'',
    cloudID:'',
    
  
   resInfo:{},
   openId:'',
    
    
  },
 onLoad() {
     let that = this;
   
     wx.cloud.callFunction({
         name: 'login',
         data: {},
         success: res => {
             var resID = res.result.openid
              wx.setStorageSync('openId',resID);
             that.setData({
                 openId:resID
                 
               })   
         },
         fail: err => {
             console.error('[云函数] [login] 调用失败', err)
         }
     })
 },
    
 onShow: function() {
     var that = this
     wx.login({
         success(res) {
             console.log(res);
             that.setData({
                 code: res.code
             })
         }
     })
    
 },
  
    
    getUserProfile(e) {
        let that = this;
        wx.getUserProfile({
            desc: '完善您的信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                console.log(res);
                Store.setStore('userInfo',res.userInfo);
                that.setData({
                   isPhone:true,
                   resInfo:res.userInfo,
                   
                });
                // that.userLogin(res);
            }
        })
    },
  //    sure() {
      
  //         // wx.setStorageSync("share", "")
  //         this.setData({
  //             show: false
  //         });
  //          wx.navigateBack();
  //     },
  
  getPhoneNumber:function(e){
     var data = e.detail;
   
    
      if(data.errMsg == "getPhoneNumber:ok"){
          this.setData({
              encryptedData:data.encryptedData,
              iv:data.iv,
              cloudID:data.cloudID,
          })
          this.login();
          
      }
      
  },
 
  login: function() {
   
    var that = this;
    
     
        var data = {
            'encryptedData':that.data.encryptedData,
            'iv':that.data.iv,
            'jsCode':that.data.code,
            'openId': that.data.openId,
            'province': this.data.resInfo.province,
              'country': this.data.resInfo.country,
               'ckName': this.data.resInfo.nickName,  
            'avatarurl': this.data.resInfo.avatarUrl,          
            'city': this.data.resInfo.city,
            'account':'',
            'invitationCode': app.globalData.custNo,
            'many2Many':'103102',  
            }        
        Api.wechatLoginApi(data).then(rs=>{  
            
            
           if(rs.code == 0){
              
               if(rs.hasOwnProperty('loginInfo')){
                    let aaa = rs.loginInfo;
                   if(aaa != null){
                       wx.setStorageSync('myCustNo',rs.loginInfo.account);
                        wx.setStorageSync('ckNo',rs.loginInfo.ckNo);
                   }
                   
               }
                 // wx.setStorageSync('baseInfo',{'ckName':"江晓运",'ckType':'104001'});
               if(rs.hasOwnProperty('baseInfo')){
                   let baseIn = rs.baseInfo;
                   if(baseIn != null){
                       wx.setStorageSync('baseInfo',rs.baseInfo);
                   }
                   
               }
             
               Store.setStore('token',rs.token);
               Store.setStore('name',that.data.resInfo.nickName); 
               if(rs.hasOwnProperty('mktCkCouponList')){
                   if(rs.mktCkCouponList.length == 0){
                       wx.navigateBack();
                   }
                   // else{
                   //     that.setData({
                   //         show:true,
                   //         sum:rs.mktCkCouponList.length
                   //     })
                   // }  
               }  
           }else{
              
                wx.showToast({ title: rs.msg, icon: 'none' });
           }                  
        });
    
  },

})
